随笔分类
获取配置文件中值:
加入依赖
<!-- 获取自定义的源数据文件 导入配置文件处理器,配置文件进行绑定就会有提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
引用这个依赖,官方中对于spring-boot-configuration-processor是这么说明的:
通过使用spring-boot-configuration-processor jar, 你可以从被@ConfigurationProperties注解的节点轻松的产生自己的配置元数据文件。
说得很清楚,自定义的元数据文件使用注解方式获取,需要先引入这个依赖。
此时编写yml便会有了代码提示
person:
name: liangye
birth: 2020/5/31
map: {k1: v1,k2: v2}
list:
- dog
- cat
- sky
friends:
name: luoye
age: 20
类中
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中所有属性和配置文件中相关的配置进行绑定
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
*
* 只有组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能 --快速功能动态绑定
*/
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
private String name;
private Date birth;
private Map<String, Object> map;
private List<Object> list;
private Friends friends;
}
1.@ConfigurationProperties对比@Value
二者默认都是从全局配置文件中获取值的
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定 | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 @Validated | 不支持 |
复杂类型的封装 | 支持 | 不支持 |
如:@Validated后用@Email来对数据进行校验
无论是yml还是property都能获取配置文件中的值
@Value仅能去获取一些基本的数据类型,像map,list,对象中的对象之类的都不支持
如果我们只是在某个业务逻辑下需要获取配置文件中的某项值,使用@Value
如果我们专门写了一个javabean来和配置文件进行映射的话,使用@ConfigurationProperties.
2.@PropertySource&@ImportResource
@PropertySource
从指定配置文件中获取值(但却是另外一种效果,会先去默认配置文件中去寻找值,找到了则会结束寻找;找不到了再去指定的配置文件中去寻找 --所以小心被覆盖的问题)
@PropertySource(value = {"classpath:person.yml"})
@ImportResource
导入Spring的配置文件,让配置文件里面的内容生效
SpringBoot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别
像让Spring的配置文件生效,加载进来;需要将@ImportResource标注在一个配置类上(可以是启动文件的配置类上)
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="helloService" class="com.base.demo.service.helloService">
</bean>
</beans>
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意:这种方式过于繁杂,SpringBoot并不推荐使用Spring的配置文件
SpringBoot推荐给容器添加组件的方式;推荐使用全注解的方式
- 配置类=====Spring的配置文件
- 用@Bean给容器来添加组件
package com.base.demo.config;
import com.base.demo.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:指明当前类是一个配置类:就是用来替代Spring配置文件
* 在Spring使用<bean></bean>标签添加组件
* 配置中使用@Bean
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到ioc容器中去,容器中这个组件默认的id就是方法名 --自动会去执行
@Bean
public HelloService helloService2(){
System.out.println("配置类@Bean给容器添加组件了");
return new HelloService();
}
}
测试:
@Autowired
ApplicationContext ioc;
@Test
void TestHelloService(){
Boolean flag = ioc.containsBean("helloService");
System.out.println(flag);
}
运行结果
2020-10-01 10:05:43.639 WARN 11776 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.base.demo]' package. Please check your configuration.
配置类@Bean给容器添加组件了
2020-10-01 10:05:46.284 INFO 11776 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-01 10:05:48.339 INFO 11776 --- [ main] com.base.demo.DemoApplicationTests : Started DemoApplicationTests in 8.507 seconds (JVM running for 11.79)
false